#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "./../../inc/login.h"
#include "./../../inc/msg_popup.h"

extern lv_obj_t * msg_box     = NULL;
extern lv_obj_t * msg_label   = NULL;
extern lv_timer_t * msg_timer = NULL;

static void set_msg_text_color_mode(MSG_TEXT_COLOR_MODE mode);
static void set_msg_long_mode(MSG_LONG_MODE mode);

// 隐藏消息框的回调函数
static void hide_msg_cb(lv_event_t * e)
{
    lv_obj_add_flag(msg_box, LV_OBJ_FLAG_HIDDEN);
    if(msg_timer) {
        lv_timer_del(msg_timer);
        msg_timer = NULL;
    }
}

// 定时器回调，自动隐藏消息框
static void timer_cb(lv_timer_t * timer)
{
    if(msg_box) {
        lv_obj_add_flag(msg_box, LV_OBJ_FLAG_HIDDEN);
    }
    if(msg_timer) {
        lv_timer_del(msg_timer);
        msg_timer = NULL;
    }
}

// 清理定时器
static void cleanup_timer(void)
{
    if(msg_timer) {
        lv_timer_del(msg_timer);
        msg_timer = NULL;
    }
}

// 设置消息框文本颜色模式
static void set_msg_text_color_mode(MSG_TEXT_COLOR_MODE mode)
{
    if(msg_label) {
        if(mode == MSG_TEXT_COLOR_RED) {
            lv_obj_set_style_text_color(msg_label, lv_color_hex(RED), 0);
        } else {
            lv_obj_set_style_text_color(msg_label, lv_color_hex(BLACK), 0);
        }
    }
}

// 设置消息框长度模式
static void set_msg_long_mode(MSG_LONG_MODE mode)
{
    if(msg_box) {
        if(mode == MSG_LONG_LONG) {
            lv_obj_set_size(msg_box, LV_PCT(50), LV_PCT(20));
        } else if(mode == MSG_LONG_SHORT) {
            lv_obj_set_size(msg_box, LV_PCT(30), LV_PCT(20));
        } else if(mode == MSG_LONG_LONGLONG) {
            lv_obj_set_size(msg_box, LV_PCT(70), LV_PCT(20));
        }
    }
}

// 显示消息框
void show_msg_box(const char * message, MSG_TEXT_COLOR_MODE color, MSG_LONG_MODE size)
{
    // 先清理之前的定时器
    cleanup_timer();

    if(msg_box == NULL) {
        create_msg_box();
    }

    // 如果窗口正在显示，先隐藏再更新内容
    lv_obj_add_flag(msg_box, LV_OBJ_FLAG_HIDDEN);

    // 设置消息内容
    lv_label_set_text(msg_label, message);

    // 设置文本颜色
    set_msg_text_color_mode(color);

    // 设置窗口长度
    set_msg_long_mode(size);

    // 直接显示窗口
    lv_obj_clear_flag(msg_box, LV_OBJ_FLAG_HIDDEN);
    lv_obj_move_foreground(msg_box);

    // 创建新的定时器
    msg_timer = lv_timer_create(timer_cb, 2000, NULL);
}

// 隐藏消息框
void hide_msg_box(lv_event_t * e)
{
    hide_msg_cb(e);
}

// 立即隐藏消息框
void hide_msg_box_immediately(void)
{
    if(msg_box) {
        lv_obj_add_flag(msg_box, LV_OBJ_FLAG_HIDDEN);
    }
    cleanup_timer();
}

void create_msg_box(void)
{
    if(msg_box != NULL) return;

    msg_box = lv_obj_create(lv_scr_act());

    lv_obj_set_size(msg_box, LV_PCT(30), LV_PCT(20));

    lv_obj_set_style_bg_color(msg_box, lv_color_hex(WHITE), 0);
    lv_obj_set_style_pad_all(msg_box, 0, 0);
    lv_obj_set_style_radius(msg_box, 10, 0);
    lv_obj_align(msg_box, LV_ALIGN_TOP_MID, 0, 20);
    lv_obj_add_flag(msg_box, LV_OBJ_FLAG_HIDDEN);

    // 添加点击隐藏功能
    lv_obj_add_event_cb(msg_box, hide_msg_cb, LV_EVENT_CLICKED, NULL);

    // 创建消息标签
    msg_label = lv_label_create(msg_box);
    lv_obj_set_size(msg_label, LV_PCT(80), LV_PCT(30));
    lv_label_set_long_mode(msg_label, LV_LABEL_LONG_WRAP);

    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(msg_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_center(msg_label);
    lv_label_set_text(msg_label, "消息内容");
}

// 销毁消息框
void destroy_msg_box(void)
{
    cleanup_timer();
    if(msg_box) {
        lv_obj_del(msg_box);
        msg_box   = NULL;
        msg_label = NULL;
    }
}
